package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import android.widget.DatePicker
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.widget.PopDialog
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.NameIdParam
import com.cheyun.netsalev3.bean.task.TaskListData
import com.cheyun.netsalev3.repository.TaskListActivityRepository
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity
import com.cheyun.netsalev3.widget.DatePickerFragment


class TaskListActivityViewModel(key:String,var repository: TaskListActivityRepository) : BaseViewModel(), PopDialog.PopDialogListener{

    var ckey = MutableLiveData<String>("today")
    var taskKey = ""
    var items= repository.loadAndroidData()

    val networkState = Transformations.switchMap(items) { repository.networkState }!!
    val total = Transformations.switchMap(items) { repository.total }!!
    //任务时间
    var day = ObservableField<String>("")
    var seriesName = ObservableField<String>("任务类型")
    var series = MutableLiveData<List<NameIdParam>>()
    var seriesId = "0"

    init {
        taskKey = key
        ckey.postValue(key)
        var ls = arrayListOf<NameIdParam>()
        ls.add(NameIdParam("沟通","1"))
        ls.add(NameIdParam("到店","2"))
        ls.add(NameIdParam("试驾","3"))
        ls.add(NameIdParam("全部","0"))
        series.postValue(ls)
//        var d = ArrayList<TaskListData>()
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        d.add(TaskListData(1))
//        listdata.postValue(d)

    }

    fun refresh(){
        var searchMap = HashMap<String, String>()
        searchMap.put("ckey",taskKey)
        if(day.get()!!.isNotEmpty()){
            searchMap.put("day",day.get()!!)
        }
        if(!seriesId.equals("0")){
            searchMap.put("tkey",seriesId)
        }
        repository.changeMap(searchMap)
        repository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }

    private var dialog: PopDialog? = null

    fun onChooseDateType(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetstart
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }

    fun onDateSetstart(view: DatePicker, year: Int, month: Int, d: Int) {
        day.set("$year-$month-$d")
        refresh()
    }

    fun onDeleteDateType(view:View){
        day.set("")
        refresh()
    }

    fun onChooseSeries(view:View){
        var content = view.context
        if (content is AppCompatActivity) {
            if (dialog == null) {
                dialog = PopDialog()
                dialog!!.listener = this
            }
            dialog?.show(content.supportFragmentManager, "pop")
        }
    }

    fun onClickTask(vewi:View,key:String){
        taskKey = key
        ckey.postValue(key)
        refresh()
    }

    fun onChooseItem(view:View,item:TaskListData){
        var intent = Intent(view.context, DefeatDetailsActivity::class.java)
        intent.putExtra("tid", item.tid)
//        intent.putExtra("nkey", item.neky)
        view.context.startActivity(intent)
    }

    override var list: MutableLiveData<List<NameIdParam>>
        get(){
            return series
        }
        set(value) { list = value}
    override var dialogType: MutableLiveData<String>
        get() {
            return MutableLiveData("list")
        }
        set(value) {dialogType = value}
    override var dialogTitle: MutableLiveData<String>
        get() {
            return MutableLiveData("选择任务类型")
        }
        set(value) {
            dialogTitle = value
        }

    override fun onClickItem(view: View, param: NameIdParam) {
        series.value?.apply {
            for (item in this) {
                item.IsSelect = param.id.equals(item.id)
            }
        }
        if(param.name.equals("全部")){
            seriesName.set("任务类型")
            seriesId = "0"
        }else {
            seriesName.set(param.name)
            seriesId = param.id.toString()
        }
        if(dialog!=null){
            dialog?.dismiss()
        }
        refresh()
    }

    override fun onDialogSubmit(view: View, param: String) {

    }


}